package com.star.common.kit.lang;

import lombok.extern.slf4j.Slf4j;
import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;

/**
 * pinyin工具类
 *
 * @author zhouhengzhe
 * @公众号 小白的Java进阶之路
 */
@Slf4j
public class PinyinKit {
    static HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();

    static {
        format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
    }

    public static String toPinyin(String hanyu) {
        try {
            return PinyinHelper.toHanYuPinyinString(hanyu, format, "", Boolean.FALSE);
        } catch (BadHanyuPinyinOutputFormatCombination e) {
            log.error("转换拼音失败", e);
            return hanyu;
        }
    }


    public static String toPINYIN(String hanyu) {
        try {
            return PinyinHelper.toHanYuPinyinString(hanyu, format, "", Boolean.FALSE).toUpperCase();
        } catch (BadHanyuPinyinOutputFormatCombination e) {
            log.error("转换拼音失败", e);
            return hanyu;
        }
    }
}
